package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.DrugBean;
import com.hxkcloud.nurse.viewholders.AddDrugViewHolder;

import java.util.List;

/**
 * Created by william_sun on 2016-10-08.
 * Description:添加修改服药记录
 */
public class AddDrugAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<DrugBean> drugs;
    private LayoutInflater mInflater;
    private onDrugDoseClickListener listener;

    public AddDrugAdapter(Context mContext, List<DrugBean> datas, onDrugDoseClickListener listener) {
        this.mContext = mContext;
        this.drugs = datas;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
    }

    public void setData(List<DrugBean> datas) {
        this.drugs = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_adddrug, null);
        AddDrugViewHolder holder = new AddDrugViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AddDrugViewHolder viewHolder = (AddDrugViewHolder) holder;

        DrugBean drug = drugs.get(position);

        viewHolder.tv_drug.setText(drug.getDrugName());

        if (!TextUtils.isEmpty(drug.getSelectDose())) {
            String doseName = "";
            for (DrugBean.DrugDose dose : drug.getDoseList()) {
                if (dose.getDoseId().equals(drug.getSelectDose())) {
                    doseName = dose.getDoseName();
                }
            }
            if (!TextUtils.isEmpty(doseName)) {
                viewHolder.tv_dose.setText(doseName);
                viewHolder.tv_dose.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
            } else {
                viewHolder.tv_dose.setText(R.string.pleaseselectdose);
                viewHolder.tv_dose.setTextColor(ContextCompat.getColor(mContext, R.color.grey_text1));
            }

        } else {
            viewHolder.tv_dose.setText(R.string.pleaseselectdose);
            viewHolder.tv_dose.setTextColor(ContextCompat.getColor(mContext, R.color.grey_text1));
        }

        viewHolder.tv_dose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDoseClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return drugs.size();
    }

    public interface onDrugDoseClickListener {
        void onDoseClick(int position);
    }
}
